package com.redsoft.spirit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.redsoft.spirit.entity.JsonResult;
import com.redsoft.spirit.entity.Page;
import com.redsoft.spirit.entity.Role;
import com.redsoft.spirit.entity.TreeNode;
import com.redsoft.spirit.service.AuthorizationService;
import com.redsoft.spirit.service.ComCodeTableService;
import com.redsoft.spirit.service.PageElementService;
import com.redsoft.spirit.service.PageService;
import com.redsoft.spirit.service.RoleService;

@RestController
public class AuthorizationController {
	
	@Autowired
	private AuthorizationService authService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private PageElementService elementService;
	
	@Autowired
	private PageService pageService;
	
	@Autowired
	private ComCodeTableService codeTableService;
	
	private Gson gson = new Gson();
	
	@RequiresRoles("admin")
	@GetMapping("/auth-moudles-pages")
	public String moudlePages() {
		List<TreeNode> tree = authService.getTreeData();
		return new JsonResult(tree).success().toString();
	}
	
	/**
	 * 根据组件id查询页面list
	 * 
	 * <pre>
	 * 根据组件id查询页面list
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 */
	@GetMapping("/auth-moudles-pages/{id}")
	public String moudlePages(@PathVariable String id) {
		List<TreeNode> tree = new ArrayList<>();
		List<Page> pages = pageService.getPagesByComponentId(id);
		
		for (Page page : pages) {
			TreeNode node = new TreeNode();
			node.setId(page.getId());
			node.setLabel(page.getPageName());
			tree.add(node);
		}
		return new JsonResult(tree).success().toString();
	}
	
	/**
	 * 查询出这个人对应的能管理的所有角色list，和拥有的所有元素list，和 这些角色已经拥有的元素list.
	 * 
	 * <pre>
	 * 查询出这个人对应的能管理的所有角色list，和拥有的所有元素list，和 这些角色已经拥有的元素list
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@GetMapping("/authorizations/pages/{pageId}")
	public String authorizations(@PathVariable String pageId, String deptId) {
		List<Role> roleList = roleService.getRoleListByDept(deptId);
		List elementList = elementService.getElementByPage(pageId);
		List authorizationList = authService.getAuthorizations(roleList, pageId);
		
		Map map = new HashMap<>();
		map.put("roleList", roleList);
		map.put("elementList", elementList);
		map.put("authorizationList", authorizationList);
		
		return new JsonResult(map).success().toString();
	}
	
	/**
	 * 更新某角色的某元素的权限
	 * 
	 * <pre>
	 * 	更新某角色的某元素的权限
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@PutMapping("/authorizations")
	public String updateAuthorizations(String authId, String elementInnerName, String pageId, String pageInnerName){
		authService.update(authId, elementInnerName, pageId, pageInnerName);
		return new JsonResult().success().toString();
	}
	
	/**
	 * 查询当前所有角色以及这些角色拥有的数据授权，以及所有的数据授权的选项
	 * 
	 * <pre>
	 * 查询当前所有角色以及这些角色拥有的数据授权（在Authorization表里，authType是data），以及当前登录人所拥有的数据授权的选项，从码表去取
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param pageId
	 * @return
	 */
	@GetMapping("/authorizations/data")
	public String authorizationData(String userId, String deptId) {
		List<Role> roleList = roleService.getRoleListByDept(deptId);
		List dataOptionsList = new ArrayList<>();
		
		if("10000".equals(userId)) {
			dataOptionsList = codeTableService.getByParentInnerName("DATA-AUTH");
		} else {
			dataOptionsList = authService.getAuthorizationDataOptions(roleList);
		}
		
		//这是其拥有的所有的，角色一一对应的。
		List authorizationList = authService.getAuthorizationData(roleList);
		
		Map map = new HashMap<>();
		map.put("roleList", roleList);
		map.put("dataOptionsList", dataOptionsList);
		map.put("authorizationList", authorizationList);
		return new JsonResult(map).success().toString();
	}
	
	/**
	 * 更新某角色的某元素的数据权限
	 * 
	 * <pre>
	 * 更新某角色的某元素的数据权限
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@PutMapping("/authorizationData")
	public String updateAuthorizationData(String authId, String elementInnerName){
		authService.updateAuthorizationData(authId, elementInnerName);
		return new JsonResult().success().toString();
	}
}